package com.shizhanzhe.gxb.brand;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.brand.activity.ThemeListActivity;
import com.shizhanzhe.gxb.brand.adapter.ThemeAdapter;
import com.shizhanzhe.gxb.brand.bean.Theme;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by YJ on 2019/1/8.
 */
public class BrandFragment extends Fragment {

    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.nodata)
    ImageView nodata;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_theme, container, false);
        unbinder = ButterKnife.bind(this, view);
        initImmersionBar();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        refresh.setRefreshHeader(new MaterialHeader(getActivity()));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list.clear();
                getData();
                refreshLayout.finishRefresh(2000);
            }
        });
        lv.setEmptyView(nodata);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                startActivity(new Intent(getContext(), ThemeListActivity.class).putExtra("themeid", list.get(i).getId()).putExtra("title",list.get(i).getName()));
            }
        });
        getData();
    }

    private List<Theme.DataBean> list = new ArrayList<>();

    private void getData() {
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.THEME(), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json)&& !"2".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        list = gson.fromJson(json, Theme.class).getData();
                        lv.setAdapter(new ThemeAdapter(getContext(), list));
                    } catch (Exception e) {
                        getData();
//                        Toast.makeText(getContext(), "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getContext(), "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    protected ImmersionBar mImmersionBar;
    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar.statusBarView(R.id.bar_0_view)
                .init();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mImmersionBar != null)
            ImmersionBar.with(this)
                    .statusBarView(R.id.bar_0_view)
                    .init();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
